package com.germinus.telcoblocks.echarts.wizards;

import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.util.Properties;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWizard;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import com.germinus.telcoblocks.echarts.projects.TelcoBlocksProjectSupport;

/**
 * New Echarts Machine Project Wizard
 * 
 * @author Pablo Muñoz
 *
 */
public class NewEChartsMachineProjectWizard extends Wizard implements
		INewWizard {

	private WizardNewProjectCreationPage page1;
	private NewEChartsMachineProjectWizardPage page2;
	private ISelection selection;

	/**
	 * Constructor for NewEChartsMachineProjectWizard
	 */
	public NewEChartsMachineProjectWizard() {
		super();
		setNeedsProgressMonitor(true);
	}
	
	
	/**
	 * Adding the page to the wizard.
	 */
	public void addPages() {
		page1 = new WizardNewProjectCreationPage("New ECharts Machine Project");
		page1.setTitle("New ECharts Machine Project");
		page1.setDescription("This wizard creates a new ECharts Machine project.");
		page2 = new NewEChartsMachineProjectWizardPage(selection);
		addPage(page1);
		addPage(page2);
		
	}

	
	/**
	 * This method is called when 'Finish' button is pressed in
	 * the wizard. We will create an operation and run it
	 * using wizard as execution context.
	 */
	public boolean performFinish() {
		final String projectName = page1.getProjectName();
		final String applicationType = page2.getApplicationType();
		final String packageName = page2.getPackageName();
		final String className = page2.getClassName();
		final String appTitle = page2.getAppTitle();
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor) throws InvocationTargetException {
				try {
					doFinish(projectName,applicationType,packageName,className,appTitle, monitor);
				} catch (CoreException e) {
					throw new InvocationTargetException(e);
				} finally {
					monitor.done();
				}
			}
		};
		try {
			getContainer().run(true, false, op);
		} catch (InterruptedException e) {
			return false;
		} catch (InvocationTargetException e) {
			Throwable realException = e.getTargetException();
			MessageDialog.openError(getShell(), "Error", realException.getMessage());
			return false;
		}
		return true;
		
	}
	
	
	/**
	 * The worker method. It will find the container, create the
	 * file if missing or just replace its contents, and open
	 * the editor on the newly created file.
	 */
	private void doFinish(
		String projectName,
		String ApplicationType,
		String PackageName,
		String ClassName,
		String AppTitle,
		IProgressMonitor monitor)
		throws CoreException {
		/*IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);*/
		Properties props = new Properties();
		props.setProperty("ApplicationType", ApplicationType);
		props.setProperty("PackageName", PackageName);
		props.setProperty("ClassName", ClassName);
		props.setProperty("AppTitle", AppTitle);
		URI location =  ResourcesPlugin.getWorkspace().getRoot().getRawLocationURI();
		IProject project = TelcoBlocksProjectSupport.createProject(projectName,location,props,monitor);
		if (project != null){
			project.refreshLocal(IResource.DEPTH_INFINITE, monitor);
			monitor.worked(1);
			return;
		}
		else{
			monitor.worked(0);
		}
	}

	/**
	 * We will accept the selection in the workbench to see if
	 * we can initialize from it.
	 * @see IWorkbenchWizard#init(IWorkbench, IStructuredSelection)
	 */
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
	}
}
